require(['highcharts','reportCommon'], function () {
	var carId,carName,titleStr="",dateType="02";
	function carChange(event, treeId, treeNode) {
		if(treeNode.type==='CAR'){
			carId = treeNode.id;
			carName = treeNode.name;
			//标题时间
			titleStr = chartTitle(dateType);
			//行为数据
			getCarAnaBehIdx(carId,dateType);
			//行为趋势图
			getCarAnaBehTrend(carId,dateType);
			//行为排名 柱状图 饼图
			getCarAnaBehRank(carId,dateType);
		}else{
			$.messager.alert("提示","请选择一辆车进行查看。");
		}
	}
	//左侧树
	commonFun.leftTreeNodeClickObj['carAnaBehaviorInfo'] = carChange;
	
	//顶部日期类型
	$(".time-select a").click(function(){
		if(carId){
			dateType = $(this).attr('val');
			//标题时间
			titleStr = chartTitle(dateType);
	        $(this).addClass("active");
	        $(this).siblings().removeClass("active");
	        //行为数据
			getCarAnaBehIdx(carId,dateType);
			//行为趋势图
			getCarAnaBehTrend(carId,dateType);
			//行为排名 柱状图 饼图
			getCarAnaBehRank(carId,dateType);
		}
    });
	
	//行为数据
	function getCarAnaBehIdx(carId,dateType){
		$.ajaxExt({
			url :"/carAna/getCarAnaBehIdx.htm",
			data : {'carId' : carId,'dateType' : dateType}
		}).done(function(data){
			$("#unBehaviorCountPer").removeClass();
			$("#behaviorRankPer").removeClass();
			if(!data){
					$("#unBehaviorCount").html("-");
					$("#unBehaviorCountPer").html("");
					$("#behaviorRank").html("-");
					$("#behaviorRankPer").html("");
			}else{
				$(data).each(function(i,item) {
					if(item.idxType=="041001"){
						$("#unBehaviorCount").html(item.idxValue);
						var per = item.idxMomPer;
						$("#unBehaviorCountPer").html(per);
						if(per.indexOf("-")!=0){
							$("#unBehaviorCountPer").addClass("icon-arrow-up");
						}else{
							$("#unBehaviorCountPer").addClass("icon-arrow-up");
							$("#unBehaviorCountPer").addClass("icon-arrow-down");
						}
					}else if(item.idxType=="041002"){
						$("#behaviorRank").html(item.idxValue);
						var per = item.idxMomPer;
						$("#behaviorRankPer").html(per);
						if(per>0){
							$("#behaviorRankPer").addClass("icon-arrow-up");
						}else if(per<0){
							$("#behaviorRankPer").addClass("icon-arrow-up");
							$("#behaviorRankPer").addClass("icon-arrow-down");
						}else{
							$("#behaviorRankPer").addClass("icon-arrow-up");
							$("#behaviorRankPer").addClass("icon-arrow-unchanged");
						}
					}
				});
			}
		});
	}
	
	//行为数据
	function getCarAnaBehTrend(carId,dateType){
		$.ajaxExt({
			url :"/carAna/getCarAnaBehTrend.htm",
			data : {'carId' : carId,'dateType' : dateType}
		}).done(function(data){
			if(data.length>0){
			var series=[],category=[],
			tmpDate = data[0].REPORT_DATE;
			for(var i=0;i<data.length;i++){
				if(data[i].REPORT_DATE == tmpDate){
					series.push({"name":data[i].TREND_VAL1,data:[]});
				}else{
					break;
				}
			}
			for(var i=0;i<data.length;i+=series.length){
				xAxisDateFormat(dateType,category,data[i].REPORT_DATE);
				for(var s=0;s<series.length;s++){
					series[s].data.push(parseInt(data[i+s].TREND_VAL2));
				}
			}
			behaviorData(titleStr,category,series);
			}else{
				behaviorData("暂无数据","","");
			}
		});
	}
	//行为数据
	function behaviorData(titleStr,category,series){
		var tickInter;
		if(dateType=="04" || dateType=="05"){
			tickInter=2;
		}else{
			tickInter=1;
		}
		$('#behaviorData').highcharts({
	        chart: {
	            type: 'column'
	        },
	        title: {
	            text: "车辆："+carName +' '+titleStr,
	            style:
             	{
             	  fontFamily: 'sans-serif',
         		  fontSize: '14px'
             	}
	        },
	        xAxis: {
	        	tickInterval: tickInter,
	            categories: category
	        },
	        yAxis: {
	            min: 0,
	            title: {
	                text: '次数'
	            },
	            stackLabels: {
	                enabled: true,
	                style: {
	                    fontWeight: 'bold',
	                    color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray'
	                }
	            }
	        },
	        credits: {
		          enabled:false
		    },
	        legend: {
	            align: 'center',
	            verticalAlign: 'bottom',  
	            y: 20,
	            floating: false,
	            backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColorSolid) || 'white',
	            borderColor: '#CCC',
	            shadow: false
	        },
	        tooltip: {
	            formatter: function() {
	                return '<b>'+ this.x +'</b><br/>'+
	                    this.series.name +': '+ this.y +'<br/>'+
	                    'Total: '+ this.point.stackTotal;
	            }
	        },
	        plotOptions: {
	            column: {
	                stacking: 'normal',
	            }
	        },
	        series: series
			});
	}
	//行为排名 柱状图 饼图
	function getCarAnaBehRank(carId,dateType){
		$.ajaxExt({
			url :"/carAna/getCarAnaBehRank.htm",
			data : {'carId' : carId,'dateType' : dateType}
		}).done(function(data){
			var behNameArr=[],behCountArr=[],behLineAvgArr=[],behaviorPct=[],
			trHtml="<tr><th width='15%'>排名</th><th width='25%'>行为名称</th><th width='15%'>次数</th><th width='20%'>占比</th><th>线路平均值</th></tr>";
			if(data.length>0){
			$(data).each(function(i,item) {
				if(item.IDX_TYPE=="041201"){
					if(item.RANK_SN<=5){
						//行为排名 填充数据
						trHtml+="<tr><td><span class='rank-tip'>"+ item.RANK_SN +"</span></td><td>"+ item.RANK_VAL1 +"</td><td>"+ item.RANK_VAL2 +"</td><td>"+item.RANK_VAL3+"</td><td>"+item.RANK_VAL4+"</td></tr>";
					}
					//饼图 填充数据
					behaviorPct.push([item.RANK_VAL1, parseInt(item.RANK_VAL2)||0]);
					//柱状图 填充数据
					behNameArr.push(item.RANK_VAL1);
					behCountArr.push(parseInt(item.RANK_VAL2)||0);
				}else if(item.IDX_TYPE=="041202"){
					behLineAvgArr.push(parseInt(item.RANK_VAL2)||0);
				}
			});
			$("#trHtml").html(trHtml);
			behaviorRadar(titleStr,behNameArr,behCountArr,behLineAvgArr);
			uncivilizedPro(titleStr,behaviorPct);
			}else{
				$("#trHtml").html("<tr><td><span>车辆："+carName+" 暂无数据</span></td></tr>");
				uncivilizedPro("暂无数据","");
				behaviorRadar("暂无数据","","","");
			}
		});
	}
	
	//饼图
	function uncivilizedPro(titleStr,behaviorPct){
		$("#uncivilizedPro").highcharts({
			chart: {
	            plotBackgroundColor: null,
	            plotBorderWidth: null,
	            plotShadow: false
	        },
	        title: {
	        	text: "车辆："+carName +' '+titleStr,
	    	    style:
	         	{
	         	  fontFamily: 'sans-serif',
	     		  fontSize: '14px'
	         	}
	        },
	        tooltip: {
	    	    pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
	        },
	        plotOptions: {
	            pie: {
	                allowPointSelect: true,
	                cursor: 'pointer',
	                dataLabels: {
	                    enabled: true,
	                    color: '#000000',
	                    connectorColor: '#000000',
	                    format: '<b>{point.name}</b>: {point.percentage:.1f} %'
	                }
	            }
	        },
	        credits: {
				enabled: false
			},
	        series: [{
	        	type: 'pie',
				name: '不文明行为比例',
				data: behaviorPct
	        }]
	    });
	}
	//柱状图
	function behaviorRadar(titleStr,behNameArr,behCountArr,behLineAvgArr){
		$('#behaviorRadar').highcharts({
		    chart: {
		    	type: 'column'
		    },
		    title: {
		    	 text: "车辆："+carName +' '+titleStr,
		    	 style:
	             	{
	             	  fontFamily: 'sans-serif',
	         		  fontSize: '14px'
	             	}
		    },
		    pane: {
		    	size: '80%'
		    },
		    xAxis: {
		        categories: behNameArr,
		        tickmarkPlacement: 'on',
		        lineWidth: 0
		    },
		    yAxis: {
		    	title: {
		    		text: '次'
		        }
		    },
		    tooltip: {
		    	shared: true,
		        pointFormat: '<span style="color:{series.color}">{series.name}: <b>{point.y:,.0f}</b><br/>'
		    },
		    credits: {
		          enabled:false
		    },
		    legend: { align: 'center', 
	            verticalAlign: 'bottom',  
	            layout: 'vertical' 
        	},
		    series: [{
		        name: '车辆次数',
		        data: behCountArr
		    }, {
		        name: '线路平均次数',
		        data: behLineAvgArr
		    }]
		});
	}
    //如果左侧树有选择车辆则直接查询该车辆数据
	business.execFnByLeftTreeSelectNode('CAR', carChange);
});